package aole.util;

import java.awt.Component;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public abstract class AbstractFile {
	private File			file;
	private String			name;
	private String			appTitle;
	private String			windowTitle;
	private boolean			dirty;
	private JFileChooser	fc;

	public static class VisqFileFilter extends FileFilter {
		private String	desc;
		private String	ext;

		public VisqFileFilter( String desc, String ext ) {
			this.desc = desc;
			this.ext = ext;
		}

		public String getExtension() {
			return ext;
		}

		@Override
		public boolean accept( File f ) {
			if( f.isDirectory() ) { return true; }
			String extension = AbstractFile.getExtension( f );
			if( extension != null ) {
				if( extension.toLowerCase().equals( ext.toLowerCase() ) ) {
					return true;
				} else {
					return false;
				}
			}

			return false;
		}

		@Override
		public String getDescription() {
			return desc;
		}

	}

	public AbstractFile( String applicationTitle ) {
		appTitle = applicationTitle;
		fc = new JFileChooser();
	}

	public boolean isDirty() {
		return dirty;
	}

	public void setClean() {
		dirty = false;
		windowTitle = (appTitle + " - " + name);
	}

	public void setDirty() {
		dirty = true;
		windowTitle = (appTitle + " - " + name + "*");
	}

	public String getFileName() {
		return name;
	}

	public String getWindowTitle() {
		return windowTitle;
	}

	public VisqFileFilter addFileFilter( String desc, String ext ) {
		VisqFileFilter ff = new VisqFileFilter( desc, ext );
		fc.addChoosableFileFilter( ff );
		return ff;
	}

	public void addFileFilter( VisqFileFilter filter ) {
		fc.addChoosableFileFilter( filter );
	}

	public void setFileFilter( VisqFileFilter filter ) {
		fc.setFileFilter( filter );
	}

	public void resetFileFilters() {
		fc.resetChoosableFileFilters();
	}

	protected abstract boolean doNew();

	protected abstract boolean doNewDialog();

	protected abstract boolean doOpen( File f );

	protected abstract boolean doSave( File f );

	public void newFile() {
		name = "Untitiled";
		file = null;
		setClean();
		doNew();
	}

	public void newFileDialog() {
		name = "Untitiled";
		file = null;
		setClean();
		doNewDialog();
	}

	public boolean open( Component parent ) {
		try {
			fc.setDialogTitle( "Open " + appTitle + " File" );
			if( file != null ) fc.setCurrentDirectory( file.getParentFile() );

			int returnVal = fc.showOpenDialog( parent );
			if( returnVal == JFileChooser.APPROVE_OPTION ) {
				file = fc.getSelectedFile();
				// parse file
				if( !doOpen( file ) ) {
					// error message
					return false;
				}
				// end parse file
				name = file.getName();
				setClean();
				return true;
			} else file = null;
		} catch( Exception e ) {
			e.printStackTrace();
			file = null;
		}
		return false;
	}

	public boolean save( Component parent ) {
		try {
			if( file == null ) saveAs( parent );
			if( file == null ) return false;
			// save file
			if( !doSave( file ) ) {
				// error message
				return false;
			}
			// end save file
			name = file.getName();
			setClean();
			return true;
		} catch( Exception e ) {
			e.printStackTrace();
		}
		return false;
	}

	public static File getSaveDirectory( Component parent ) {
		File f = null;
		JFileChooser fc = new JFileChooser();
		fc.setDialogTitle( "Save to directory ..." );
		fc.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );

		int returnVal = fc.showSaveDialog( parent );
		if( returnVal == JFileChooser.APPROVE_OPTION ) {
			f = fc.getSelectedFile();
		}

		return f;
	}

	public static File getSaveFile( Component parent, String desc, String ext ) {
		File f = null;
		JFileChooser fc = new JFileChooser();
		fc.setDialogTitle( "Save to file ..." );
		fc.setAcceptAllFileFilterUsed( false );
		VisqFileFilter ff = new VisqFileFilter( desc, ext );
		fc.addChoosableFileFilter( ff );

		int returnVal = fc.showSaveDialog( parent );
		if( returnVal == JFileChooser.APPROVE_OPTION ) {
			f = fc.getSelectedFile();
			if( getExtension( f ) == null )
				f = new File( f.getPath() + "." + ff.getExtension() );
		}

		return f;
	}

	public boolean saveAs( Component parent ) {
		fc.setDialogTitle( "Save " + appTitle + " File" );
		if( file != null ) fc.setCurrentDirectory( file.getParentFile() );

		int returnVal = fc.showSaveDialog( parent );
		if( returnVal == JFileChooser.APPROVE_OPTION ) {
			file = fc.getSelectedFile();
			VisqFileFilter ff = (VisqFileFilter) fc.getFileFilter();
			if( getExtension( file ) == null ) {
				file = new File( file.getPath() + "." + ff.getExtension() );
				fc.setSelectedFile( file );
			}
			save( parent );
			return true;
		} else file = null;
		return false;
	}

	public static String getExtension( File f ) {
		String ext = null;
		String s = f.getName();
		int i = s.lastIndexOf( '.' );

		if( i > 0 && i < s.length() - 1 ) {
			ext = s.substring( i + 1 ).toLowerCase();
		}
		return ext;
	}
}